package com.chenjt.juc.three_completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/7/6 14:51
 */
public class CompletableFutureCombineDemo {

    public static void main(String[] args) {

        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t-----启动");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 10;
        });

        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t-----启动");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 20;
        });


        /**
         * 先完成的先来这里等着
         * 输出结果：
         * ForkJoinPool.commonPool-worker-1	-----启动
         * ForkJoinPool.commonPool-worker-2	-----启动
         * 开始两个结果合并
         * 30
         */
        CompletableFuture<Integer> result = completableFuture1.thenCombine(completableFuture2, (x, y) -> {
            System.out.println("开始两个结果合并");
            return x + y;
        });

        System.out.println(result.join());

    }

}
